
#import <appkit/appkit.h>
#import <dbkit/dbkit.h>
#import <misckit/misckit.h>
#import "MiscTableController.h"

//--------------------------------------------------------------------------------------
	@implementation MiscTableController
//--------------------------------------------------------------------------------------
	
	/*" The #MiscTableController class allows displaying and editing lists
		with multivalue rows. The row objects can be of any arbitrary class as
		long as they conform to the #MiscTCRow protocol. HashTables are a 
		good choice.
		
		%{<<more later>>} "*/


//--------------------------------------------------------------------------------------
	- init;
//--------------------------------------------------------------------------------------

	/*"	Initialises a new #MiscTableController intance. "*/

	{
	[super init];
	rows=[[MiscList allocFromZone:[self zone]] init];
	return self;
	}
	
	
//--------------------------------------------------------------------------------------
	- free
//--------------------------------------------------------------------------------------

	/*"	Frees the #MiscTableController and %all row objects. "*/

	{
	[rows freeObjects];
	rows=[rows free];
	return [super free];
	}


//--------------------------------------------------------------------------------------
	- (MiscList *)rows;
//--------------------------------------------------------------------------------------
	
	/*" Returns the internal #MiscList Objects that holds the rows. 
		Consider it %read-only, changes can confused the controller! "*/
	
	{
	return rows;
	}


//--------------------------------------------------------------------------------------
	- (unsigned int)rowCount;
//--------------------------------------------------------------------------------------

	/*" Returns the number of rows in the #DBTableView. "*/
	
	{
	return [rows count];
	}


//--------------------------------------------------------------------------------------
	- setTableView:(DBTableView *)aView withIdentifiers:(NXAtom *)ids;
//--------------------------------------------------------------------------------------

	/*" Associates the Controller with a #DBTableView. #Identifiers must be
		all %NULL terminated array of #NXAtoms that are used to identify
		the columns. These values are passed as key in the delegate methods.
		
		Returns #nil if the identifier array contains too many or too few
		elements. "*/
		
	{
	int		colNo;
	NXAtom	*identifierP;

	for(colNo=0,identifierP=ids;*identifierP;identifierP++)
		colNo++;
	if(colNo!=[aView columnCount])
		return nil;

	tableView=aView;
	identifiers=ids;
	for(colNo=0,identifierP=identifiers;*identifierP;identifierP++)
		[[tableView columnAt:colNo++] setIdentifier:(id)(*identifierP)];
	[tableView setDataSource:self];
	[tableView reloadData:self];
	return self;
	}


//--------------------------------------------------------------------------------------
	- (DBTableView *)tableView;
//--------------------------------------------------------------------------------------

	/*"	Returns the #DBTableView that the controller is connected to. "*/

	{
	return tableView;
	}


//--------------------------------------------------------------------------------------
	- (NXAtom *)identifiers;
//--------------------------------------------------------------------------------------
	
	/*" Return the array of #NXAtoms which are used to identify the columns in
		the #DBTableView. "*/
	
	{
	return identifiers;
	}
	

//--------------------------------------------------------------------------------------
	- setDelegate:anObject;
//--------------------------------------------------------------------------------------
	
	/*"	Sets the delegate object. "*/
	
	{
	delegate=anObject;
	return self;
	}
	

//--------------------------------------------------------------------------------------
	- delegate;
//--------------------------------------------------------------------------------------
	
	/*"	Returns the delegate object. "*/
	
	{
	return delegate;
	}


//-----------------------------------------------------------
	- addRow:(id <MiscTCRow>)row;
//-----------------------------------------------------------

	/*" Adds a new row object and redisplays the #DBTableView. "*/

	{
	[rows addObject:[(id)row copyFromZone:[self zone]]];
	[tableView reloadData:self];
	
	return self;
	}
	

//-----------------------------------------------------------
	- addRowsFrom:(List *)list;
//-----------------------------------------------------------

	{
	int	idx;
	
	for(idx=0;idx<[list count];idx++)
		[rows addObject:[[list objectAt:idx] copyFromZone:[self zone]]];
	[tableView reloadData:self];

	return self;
	}
	


//--------------------------------------------------------------------------------------
	- removeSelectedRow:sender;
//--------------------------------------------------------------------------------------

	/*" Removes the currently selected row (if there is one) and redisplays the
		#DBTableView."*/

	{
	int	row=[tableView selectedRow];
	
	if(row<0)
		return nil;
	[rows removeObjectAt:row];
	if(sender)
		[tableView reloadData:self];
	return self;	
	}
	


//--------------------------------------------------------------------------------------
	- empty:sender;
//--------------------------------------------------------------------------------------

	/*" Removes all rows, frees the objects and redisplays the #DBTableView."*/
	{
	[rows freeObjects];
	[rows empty];
	if(sender)
		[tableView reloadData:self];
	return self;
	}
	
			
//--------------------------------------------------------------------------------------
	- getValueFor:identifier at:(unsigned int)aPosition into:aValue;
//--------------------------------------------------------------------------------------

	/*" Method from the DBDataSource Protocol. "*/

	{
	[aValue setStringValue:[[rows objectAt:aPosition] valueForKey:identifier]];
	return self;
	}


//--------------------------------------------------------------------------------------
	- setValueFor:identifier at:(unsigned int)aPosition from:aValue;
//--------------------------------------------------------------------------------------

	/*" Method from the DBDataSource Protocol. "*/

	{
	[[rows objectAt:aPosition] insertKey:identifier value:(void *)[aValue stringValue]];
	if([delegate respondsTo:@selector(miscTableController:valueDidChangeFor:at:)])
		[delegate miscTableController:self valueDidChangeFor:identifier at:aPosition];
	return self;
	}	


//--------------------------------------------------------------------------------------
	- miscTableController:sender valueDidChangeFor:identifier at:(unsigned int)position;
//--------------------------------------------------------------------------------------

	/*" Delegate method. Sent to the delegate whenever a row changes. Delegate
		doesn't have to implement this method. "*/

	{
	return self;
	}


//--------------------------------------------------------------------------------------
	@end
//--------------------------------------------------------------------------------------

	